package com.yjx.managedemo.controller;

import com.sun.net.httpserver.HttpServer;
import com.yjx.managedemo.re.Result;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.net.http.HttpRequest;
import java.util.UUID;

@Controller
@RequestMapping("upload")
public class UploadController {

    @RequestMapping("uploadImage")
    @ResponseBody
    public Result<String> uploadImage(@RequestParam("file") MultipartFile file) throws IOException {
        File file1=new File(System.getProperty("user.dir")
                + "/src/main/resources/static/pic");
        String originalFilename = file.getOriginalFilename();
        String newFilename = UUID.randomUUID()+originalFilename.substring(originalFilename.lastIndexOf("."));
        File targetFile=new File(file1.getAbsolutePath()+"\\"+newFilename);
        file.transferTo(targetFile);
        int test;
        return Result.success(newFilename);
    }
}
